Welcome back.
I hope your quiz went well.
And now we're going to practice a little bit using RBC's script signals or AKA events.
For this practice activity, we're going to be using an instance called a proximity prompt.
We'll dive more into this instance in a future lecture, but for now we want to utilize an event in
the instance called trigger.
And this will get fired when a player interacts and triggers the prompt.
So it says the event is triggered when the prompt key slash button is pressed, or after a specified
amount of time holding the button if the whole duration is used.
So for this practice activity, I would like for you to have a script that listens to a proximity prompts
triggered event and then do something in the game, whatever you'd like.
I'm going to have my prompt change a parts color to something random, but feel free to do something
else if you'd like.
So back in studio, let's create a brand new part, and then let's go ahead and add a proximity prompt
instance inside of the part.
And now we're going to need a script to listen to this prompts event.
So take a few minutes, do the practice activity and then I will show you my solution next.
All right.
To make this quick and easy, let's just go ahead and add a new script inside of our part and inside
of our script.
Let's go ahead and make a reference to the part so we can have a variable called part that's equal to
script dot parent.
And then we can also have a reference to our proximity prompt.
So I'll just call this variable prompt.
And that's going to be equal to our part dot proximity prompt.
And then like I said there is an event inside of this prompt called triggered.
And we want to go ahead and use the connect function to connect a function to listen to this event.
And this function, if we take a look, is going to be passed the player who triggered the proximity
prompt.
If you want to mess around and see what you can do with the player, go ahead.
But what I'm going to do here is I'm going to set the parts color to a random color every single time
the prompt is triggered.
So let me create a variable for storing a random number generator.
We'll do random dot new.
And then when the prompt is triggered, we can simply do part dot color is going to be equal to a new
color three.
So we'll do color three dot new.
And then we can use our random number generator and call the next number function to generate a random
number between 0 and 1.
And we'll just do that three.
Time for the red, green and blue color property.
And we should be good to go.
So if I go and play test my game and here's my character, if I interact with the prompts, look at
that part.
Color changes.
So we got a red or we got kind of an orange.
We got a purple.
Now green.
Pretty cool.
And it is firing that event every single time I'm interacting with this prompt.
Let's have a little bit more fun with this event.
When we change the color, let's go ahead and clone our part and basically have it duplicate itself.
So we have another color changing part, and every single time the prompt is activated, we'll just
create more and more parts.
So the easiest way we could do that is we could just simply refer to our part and make a clone of it.
So we'll make a variable called clone.
And that's going to be equal to part clone.
And then let's go ahead and change this clones position to be equal to the part's position.
But we're going to offset it on the y axis.
Let's just do an offset on the y axis of like uh, I don't know.
Let's do four studs.
And then once we set the position, we need to set this new clone's parent into the workspace.
Because when we clone the part right now, its parent is going to be set to nil.
You won't be able to see it anywhere.
And then once we set its parent to the workspace, we'll be able to see it in the game.
So now if I go and interact with my part, as you can see, we've just created another new part.
And then if I interact with that one, it's going to create another new part.
And if I interact with the one on top, it'll create another new part.
Pretty cool.
Let's actually go ahead and make these parts unanchored.
So I'll just select each of these and disable the anchored property on the server side.
And then let's go back to the client and let's just start spamming these buttons.
So now if I do that I am creating a whole bunch of different parts.
And they just keep duplicating themselves.
And I'm able to keep continually cloning more and more parts.
And then of course, if you have more players in the game, all your other players could be interacting
with these parts as well and just creating more of them.
So this looks kind of funny.
And like I said, you could really do whatever you want.
So instead of maybe cloning the part, maybe we want to play like a fart sound because we're goofy like
that.
So let's go to the toolbox and let's search through the audio and let's just search for a fart.
And let's see.
Here we go.
Let's see what this one sounds like.
Perfect.
So let's go ahead and insert this sound into our part.
And then just to make sure it doesn't play for like everyone, I'm just going to set the rolloff max
distance to like 50 studs.
And then what we can go ahead and do is we can refer to this sound in our script.
We'll make a variable called sound.
And that's going to be equal to part dot.
And the name is fart meme.
There we go.
And then when the prompt is triggered we can refer to our sound and simply call the play function on
it to play it in the game.
So now every time I interact with my part.
There you go.
We play a fart sound now.
So I encourage you to continue messing around with this event.
See what else you can make.
Mess around.
Maybe with the player, the player's character spawn parts.
Do whatever you'd like.
The most important thing is that you're practicing and you're experimenting.
And if you ever encounter any mistakes or you encounter an error, that is a great opportunity to practice
debugging as well.
Hope you enjoyed this lecture and I will see you in the next one.